package net.monare.mifss.admin.ui.mod;

import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TextField;
import net.monare.mifss.MifssMessages;
import net.monare.mifss.admin.ui.valid.EmptyStringValidator;
import net.monare.mifss.configurations.pojo.StorageSetDef;

/**
 * Created by jernej on 4/9/14.
 */
public class StorageSetDefUiModule  extends FormLayout {

    private TextField storageSetName;
    private TextField storageSetDescription;

    public StorageSetDefUiModule() {
        initElements();
    }




    private void initElements(){

        String stringValidationMsg = MifssMessages.getMessage("validation.empty.string");

        storageSetName = new TextField(MifssMessages.getMessage("label.storage.set.name"));
        storageSetName.setWidth("90%");
        storageSetDescription = new TextField(MifssMessages.getMessage("label.storage.set.description"));
        storageSetDescription.setWidth("90%");

        // setup validators
        storageSetName.addValidator(new EmptyStringValidator(stringValidationMsg));

        addComponent(storageSetName);
        addComponent(storageSetDescription);
    }


    public StorageSetDef getStorageSetDef(){

        StorageSetDef storageSetDef = new StorageSetDef();

        storageSetDef.setLocked(false);

        if (storageSetName.isValid()) {
            storageSetDef.setName(storageSetName.getValue());
        } else {
            return null;
        }

        if (storageSetDescription.isValid()) {
            storageSetDef.setDescription(storageSetDescription.getValue());
        } else {
            return null;
        }

        return  storageSetDef;
    }

}
